package com.woniuxy.meal.handler;

import com.woniuxy.meal.common.Result;
import com.woniuxy.meal.exception.WoniuxyException;
import com.woniuxy.meal.exception.addressException.AddressException;
import com.woniuxy.meal.exception.changeStateException.ChangeStateException;
import com.woniuxy.meal.exception.foodTypeException.FoodTypeException;
import com.woniuxy.meal.exception.orderException.OrderException;
import com.woniuxy.meal.exception.parameterValidateException.ParameterValidateException;
import com.woniuxy.meal.exception.userCartException.UserCartException;
import com.woniuxy.meal.exception.userCartException.UserCartExceptionCode;
import com.woniuxy.meal.exception.userException.UserException;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.MalformedJwtException;
import io.jsonwebtoken.security.SignatureException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.Valid;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(UserException.class)
    public  Result handleUserException(UserException e){
        return Result.fail(e.getCode(),e.getMessage());
    }

    @ExceptionHandler(UserCartException.class)
    public  Result handleUserException(UserCartException e){
        return Result.fail(e.getCode(),e.getMessage());
    }

    @ExceptionHandler(OrderException.class)
    public  Result handleOrderException(OrderException e){
        return Result.fail(e.getCode(),e.getMessage());
    }


    @ExceptionHandler(AddressException.class)
    public  Result handleAddressException(AddressException e){
        return Result.fail(e.getCode(),e.getMessage());
    }

    @ExceptionHandler(FoodTypeException.class)
    public  Result handleFoodTypeException(FoodTypeException e){
        return Result.fail(e.getCode(),e.getMessage());
    }

    @ExceptionHandler(ChangeStateException.class)
    public  Result handleChangeStateException(ChangeStateException e){
        return Result.fail(e.getCode(),e.getMessage());
    }
    @ExceptionHandler(ParameterValidateException.class)
    public  Result handleWoniuException(ParameterValidateException e){
        return Result.fail(e.getCode(),e.getMessage());
    }


    @ExceptionHandler({MalformedJwtException.class, SignatureException.class})
    public Result handlerJwtException(Exception e) throws Exception {
        e.printStackTrace();
        return Result.fail(5002,"token无效");
    }

    @ExceptionHandler({ExpiredJwtException.class})
    public Result handlerExpiredJwtException(Exception e) throws Exception {
        e.printStackTrace();
        return Result.fail(5001,"token已过期");
    }

    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e){

        if (e instanceof MethodArgumentNotValidException){
            return Result.fail(5090,"参数错误");
        }
        else {
            e.printStackTrace();
            return Result.fail(500,e.getMessage());
        }

    }

}
